C语言前辈来看一下`~有些问题不明白

来源:百度知道 编辑:UC知道 时间:2024/09/20 16:44:26
#define N 20
#include <stdio.h>
void cpy(char *p,char *p);
main()
{
char a[N],b[N],*p=a,*q=b;
printf("inter..\n");
scanf("%s",a);
cpy(*p,*q);
printf("%s",q);
}
void cpy(char *p,char *q)
{
while(*p!='\0') 错误 while(*(p-1)!='\0') 正确~
{
*q=*p;
p++;q++;
{
*q=*p;
p++;q++;
}
}
着是为什么呀?~前辈解释一下`~

帮你改好了

#include <stdio.h>
#define N 20

void cpy(char *p, char *q);

int main()
{
char a[N], b[N], *p, *q;
p=a;
q=b;
printf("enter..\n");
scanf("%s", a);
cpy(p, q);
printf("%s\n", q);
return 0;
}

void cpy(char *p, char *q)
{
while(*p!='\0')
{
*q=*p;
q++;
p++;
}
*q='\0';
}

这能执行吗?错误太多了吧!

因为数组是从0到n-1的啊。这样一共储存n个元素。。第n个已经不是这个数组了。到了下一个地址了

老大你这个程序是对的吗????
函数都没有